var eap_manager_menu = {

	gotoPageSetting : function(obj) {
		var $btn = $(obj);
		var $modelNames = $btn.closest('.jform').find('input[name="model.i18n.cnName"],input[name="model.i18n.cnName"]');
		// TODO: get current page local name
		var menuName = $modelNames.eq(0).val();
		var $ctn = $btn.closest('.modelButton');
		var path = $('.modelPath input', $ctn).val();
		if (path.trim() != '') {
			var $form = $(obj).closest('.jform');
			var options = {
				data: {
					_resultCode:'$json'
				},
				success: function() {
					path = encodeURIComponent(path);
					openBlank($btn, g_base_site + '/espirit/eap/pagelayout/page._addByPath.html?_layout=whole&pageName=' + menuName + '&path=' + path);
				}
			}
			ajaxSubmit($form, options);
		} else {
			showError(menu_text['model.espirit.eap.manager.menu.selectPath']);
		}
	},
	removeMethod : function(obj) {
		var $ctn = $(obj).closest('.modelButton');
		if ($ctn.prev('.modelButton').length != 0 || $ctn.next('.modelButton').length != 0) {
			$ctn.remove();
		} else {
			$ctn.find('input').val('');
			$ctn.find('.methodLabel').attr('required', false);
		}
	},
	addMethod : function(obj) {
		var $ctn = $(obj).closest('.modelButton');
		var $newCtn = $ctn.clone();
		var inputs = $newCtn.find('input').val('');
		$newCtn.find('.methodLabel').attr('required', true);
		$ctn.after($newCtn);
		
		this.rebuildNameIndex($ctn.closest('.pathCtn'));
	},
	rebuildNameIndex : function($ctn) {
		$ctn.find('.modelButton').each(function(rowIndex) {
			$(this).find('input').each(function() {
				var $this = $(this);
				var oldName = $this.attr('name');
				if (oldName) {
					var newName = oldName.replace(/(\w*\[)\d+(\]\w*)/, "$1" + rowIndex + "$2");
					$this.attr('name', newName);
				}
			});
		});
	},

	initMenuPage : function() {
		var $activeDom = getActiveDom();
		var $parentCmb = $('[cmbname="model.parent.id"]', $activeDom);
		var $menuName = $('[name="model.i18n.name"]', $activeDom);
		
		$parentCmb.closest('.jcombobox').data('beforeRequest', function(req) {
			req.filterProperty = 'page';
			req.filter = 'null';
			return req.pageNo + ',' + req.query;
		});
		
		// show / hide the path container according to the parent menu, it uses firstChildPath to store the root model path
		$parentCmb.on('change', function() {
			if ($(this).val() != '') {
				$('#menuMethodCtn').show();
				var menuPath = $(this).data('comboItem').firstChildPath;
				var index2 = menuPath.lastIndexOf('.');
				var index1 = menuPath.lastIndexOf('/', index2 - 1);
				if (index1 > 0 && index2 > 0) {
					var pathPrefix = menuPath.substring(0, index1 + 1) + 'xxx.';
					var pathSuffix = menuPath.substring(index2);
					// replace all the child path (only replace the parent category)
					$('#menuMethodCtn .modelPath input').each(function() {
						var $this = $(this);
						var curPath = $this.attr('oldValue');
						if (!curPath) {
							curPath = $this.val();
							$this.attr('oldValue', curPath);
						}
						if (curPath && curPath.indexOf('/') == -1) {
							$this.val(pathPrefix + curPath + pathSuffix);
						}
					});
					
					$menuName.triggerHandler('blur');
				}
			} else {
				$('#menuMethodCtn').hide();
			}
		});
		if ($parentCmb.val() == '') {
			$('#menuMethodCtn').hide();
		}
		
		function replaceXXX2NewMethod(oldPath, newMethod) {
			var modelMethodIndex = oldPath.lastIndexOf('/');
			if (modelMethodIndex > 0) {
				var modelEndIndex = oldPath.indexOf('.', modelMethodIndex);
				if (modelEndIndex > 0) {
					var newPath = oldPath.substring(0, modelEndIndex + 1) + newMethod + '.html';
					return newPath;
				}
			}
			return null;
		}
		
		
		// get the chinese letter name when inputting the name
		if ($menuName.val() == '') {
			var updateMenuPath = function(data) {
				var comboItem = $parentCmb.data('comboItem');
				
				var firstChildPath = comboItem.firstChildPath;
				var index = firstChildPath.lastIndexOf('/');
				var pathPrefix = firstChildPath.substring(0, index + 1);
				var pathSuffix = '.html';
				
				$('#menuMethodCtn .modelPath input').each(function() {
					// replace the path with the new sub category
					var $this = $(this);
					var curPath = $this.attr('oldValue');
					if (!curPath) {
						curPath = $this.val();
						$this.attr('oldValue', curPath);
					}
					if (curPath && curPath.indexOf('/') == -1) {
						$this.val(pathPrefix + data + '.' + curPath + pathSuffix);
					}
				});
			}
			
			// change menu name
			$menuName.on('blur', function() {
				var comboItem = $parentCmb.data('comboItem');
				if (comboItem ==  null) {
					return;
				}
				
				var val = $(this).val();
				var url = g_base_site + '/espirit/eap/manager/menu.getChineseSpell.html?_resultCode=$json&name=' + val;
				$.ajax({
					url: url,
					success:function(data) {
						updateMenuPath(data);
					}
				});
			});
		}
		
		// change the url according to the selected method
		getActiveDom().on('autocompletechange', '.methodLabel input[type="text"]', function() {
			var newMethod = $(this).prev('input').val();
			if ($.isNumeric(newMethod)) {
				return;
			}
			if (newMethod == '') {
				newMethod = 'xxx';
			}
			var $pathInput = $(this).closest('.modelButton').children('.modelPath').find('input');
			var oldPath = $pathInput.val();
			if (oldPath == '') {
				oldPath = $('#menuMethodCtn .modelPath input').eq(0).val();
			}
			var newPath = replaceXXX2NewMethod(oldPath, newMethod);
			if (newPath != null) {
				$pathInput.val(newPath);
			}
		});
		
		getActiveDom().find('.modelAccess').attr('title', menu_text['model.espirit.eap.manager.menu.actPermission']);
	}
}